package com.shankungfu.main;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.shankungfu.GlobleConstant;
import com.shankungfu.R;
import com.shankungfu.bean.RegisterBean;
import com.shankungfu.bean.RegisterListBean;
import com.shankungfu.bean.RisgterListDataBean;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.WebService;

public class RegisterNextActivity extends Activity implements OnClickListener {

	private RisgterListDataBean listDataBean;
	private String area = "教育";
	private String group = "儿童";
	private String skill = "教学";
	private ArrayList<String> userData;
	private TextView gongyilingyu;
	private TextView join_poeple_list;
	private TextView skillTV;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register2_layout);

		userData = getIntent().getStringArrayListExtra(
				RegisterActivity.USERDATA);
		
		
		gongyilingyu = (TextView) findViewById(R.id.gongyilingyu);
		join_poeple_list = (TextView) findViewById(R.id.join_poeple_list);
		skillTV = (TextView) findViewById(R.id.skill);
		
		Button bt = (Button) findViewById(R.id.goback);
		bt.setOnClickListener(this);
		getRegisterList();
	}

	private void getRegisterList() {
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "listes");

		AsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {

			@Override
			public void onSuccess(String content) {
				if (!TextUtils.isEmpty(content)
						&& content.contains("\"result\":0")) {
					RegisterListBean registerListBean = JSON.parseObject(
							content, RegisterListBean.class);

					listDataBean = registerListBean.getRisgterListData();
					String[] categoryData = listDataBean.getCategory();
					String[] fitsData = listDataBean.getFits();
					String[] skillData = listDataBean.getSkills();
				} else {
					ToastUtil.showMessageText(RegisterNextActivity.this,
							"注册失败，请重新尝试");
				}
			}

			@Override
			public void onFailure(String content) {
			}
		};
		httpclient.request(GlobleConstant.REGISTER_LIST, params,
				responseHandler);
	}

	private void registerUser() {
		String username = userData.get(0);
		String user_email = userData.get(1);
		String user_password = userData.get(2);

		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "register");

		params.add("user_name", username);
		params.add("email", user_email);
		params.add("password", user_password);

		params.add("area", area);
		params.add("group", group);
		params.add("skill", skill);

		MyAsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				dialog.dismiss();
				if (!TextUtils.isEmpty(content) ) {
					RegisterBean registerBean = JSON.parseObject(content,
							RegisterBean.class);
					
					if (content.contains("\"result\":0")) {
						ToastUtil.showMessageText(RegisterNextActivity.this,
								registerBean.getAddinfo());
						setResult(RegisterActivity.REGISTER_OK);
						finish();
						return;
					} else {
						ToastUtil.showMessageText(RegisterNextActivity.this,
								registerBean.getMsg());
					}

				} else {
					ToastUtil.showMessageText(RegisterNextActivity.this,
							"注册失败，请重新尝试");
				}
			}

			@Override
			public void onFailure(String content) {
				dialog.cancel();
				ToastUtil.showMessageText(RegisterNextActivity.this,
						"注册失败，请重新尝试");
			}
		};
		responseHandler.setShowNotNetToast(false);
		httpclient.request(GlobleConstant.REGISTER, params, responseHandler);
	}

	private void showListDialog(final int id, final String[] listData) {
		AlertDialog.Builder listDia = new AlertDialog.Builder(
				RegisterNextActivity.this);
		listDia.setTitle(null);
		listDia.setItems(listData, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				switch (id) {
				case R.id.gongyilingyu_rl:
					area = listData[which];
					gongyilingyu.setText(area);
					break;
				case R.id.join_poeple_list_rl:
					group = listData[which];
					join_poeple_list.setText(group);
					break;
				case R.id.skill_rl:
					skill = listData[which];
					skillTV.setText(skill);
					break;
				}
			}
		});
		listDia.create().show();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		setResult(RegisterActivity.REGISTER_FAIL);
		finish();
	}
	
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.goback:
			onBackPressed();
			break;
		case R.id.gongyilingyu_rl:
			showListDialog(v.getId(), listDataBean.getCategory());
			break;
		case R.id.join_poeple_list_rl:
			showListDialog(v.getId(), listDataBean.getFits());
			break;
		case R.id.skill_rl:
			showListDialog(v.getId(), listDataBean.getSkills());
			break;
		case R.id.register_button:
			registerUser();
			dialog = new ProgressDialog(this);
			dialog.setMessage("请稍后...");
			dialog.show();
			
			// GeneralDialog dialog = new GeneralDialog(this,
			// R.style.Transparent_Dialog);
			// dialog.show();
			break;
		}

	}
}
